<template>
    <el-container>
        <el-aside width="40%">
            <el-container>
                <el-header>
                    <div class="left-panel">
                        <el-button type="primary" icon="el-icon-plus" @click="project_add"></el-button>
                    </div>
                </el-header>
                <el-main class="nopadding">
                    <scTable ref="table" :apiObj="project.apiObj" row-key="id" @current-change="currentChange" stripe highlightCurrentRow @row-click="rowClick">
                        <sc-table-column label="序号" width="65" type="index"></sc-table-column>
                        <el-table-column label="项目名称" prop="projectName" min-width="200"></el-table-column>
                        <el-table-column label="数据库类型" prop="dbType" width="140" sortable :formatter="formatter"></el-table-column>
                        <el-table-column label="创建日期" prop="createTime" width="180" sortable></el-table-column>
                        <el-table-column label="操作" fixed="right" width="180">
                            <template #default="scope">
                                <el-button-group>
                                    <el-button type="primary" @click.stop="openDictDrawer(scope.row, scope.$index)" size="small" icon="el-icon-setting"></el-button>
                                    <el-button type="info" @click.stop="project_show(scope.row, scope.$index)" size="small" icon="el-icon-view"></el-button>
                                    <el-button type="primary" @click.stop="project_edit(scope.row, scope.$index)" size="small" icon="el-icon-edit"></el-button>
                                    <el-popconfirm title="确定删除吗？" @confirm="project_del(scope.row, scope.$index)">
                                        <template #reference>
                                            <el-button type="danger" size="small" icon="el-icon-delete" @click.stop></el-button>
                                        </template>
                                    </el-popconfirm>
                                </el-button-group>
                            </template>
                        </el-table-column>
                    </scTable>
                </el-main>
            </el-container>
        </el-aside>
        <el-container>
            <el-header>
                <div class="left-panel">
                    <el-button type="primary" icon="el-icon-plus" @click="database_add" :disabled="selection === undefined"></el-button>
                </div>
            </el-header>
            <el-main class="nopadding">
                <scTable ref="dbtable" :apiObj="database.apiObj" :params="listDbApiParams" row-key="id" stripe>
                    <sc-table-column label="序号" type="index" width="65"></sc-table-column>
                    <el-table-column label="数据库名称" prop="dbName" width="200"></el-table-column>
                    <el-table-column label="数据库类型" prop="dbType" width="140" :formatter="formatter"></el-table-column>
                    <el-table-column label="数据库注释" prop="dbComment" min-width="150"></el-table-column>
                    <el-table-column label="创建时间" prop="createTime" width="180" sortable></el-table-column>
                    <el-table-column label="操作" fixed="right" width="180">
                        <template #default="scope">
                            <el-button-group>
                                <el-button type="primary" @click.stop="openDrawer(scope.row, scope.$index)" size="small" icon="el-icon-setting"></el-button>
                                <el-button type="info" @click.stop="database_show(scope.row, scope.$index)" size="small" icon="el-icon-view"></el-button>
                                <el-button type="primary" @click.stop="database_edit(scope.row, scope.$index)" size="small" icon="el-icon-edit"></el-button>
                                <el-popconfirm title="确定删除吗？" @confirm="database_del(scope.row, scope.$index)">
                                    <template #reference>
                                        <el-button type="danger" size="small" icon="el-icon-delete" @click.stop></el-button>
                                    </template>
                                </el-popconfirm>
                            </el-button-group>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

    <el-drawer v-model="drawer.module" title="模块设计" direction="rtl" size="80%" destroy-on-close @closed="drawerClose">
        <module-table :projectId="drawer.projectId" :dbId="drawer.dbId" :dbType="drawer.dbType"/>
    </el-drawer>

    <el-drawer v-model="drawer.dict" title="项目字典" direction="rtl" size="80%" destroy-on-close @closed="drawerClose">
        <project-dict :projectId="drawer.projectId"/>
    </el-drawer>

    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
    <db-dialog v-if="dialog.dbSave" ref="dbDialog" @success="handleDbSaveSuccess" @closed="dialog.dbSave=false"></db-dialog>
</template>

<script>
import moduleTable from './module-table-list'
import projectDict from './project-dict-list'
import saveDialog from './project'
import dbDialog from './database'

export default {
    name: 'project',
    components: {
        projectDict,
        moduleTable,
        saveDialog,
        dbDialog
    },
    data() {
        return {
            project: {
                apiObj: this.$API.design.project
            },
            database: {
                apiObj: this.$API.design.database
            },
            dialog: {
                save: false,
                dbSave: false
            },
            selection: null,
            listDbApiParams: {},
            drawer: {
                module: false,
                dict: false,
                projectId: null,
                dbId: null,
                dbType: null
            },
            dictMap: {},
            props: {
                dbType: 'DB'
            }
        }
    },
    mounted() {
        this.getDictMap()
    },
    methods: {
        //添加
        project_add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        //编辑
        project_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //查看
        project_show(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('show').setData(row)
            })
        },
        //删除
        async project_del(row) {
            const res = await this.$API.design.project.delete(row.id);
            if (res.code === 200) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                await this.$alert(res.msg, "提示", {type: 'error'})
            }
        },
        //本地更新数据
        handleSaveSuccess(data, mode) {
            if (mode === 'add') {
                this.$refs.table.refresh()
            } else if (mode === 'edit') {
                this.$refs.table.refresh()
            }
        },
        rowClick(row) {
            if (this.listDbApiParams.projectId !== row.id) {
                this.listDbApiParams.projectId = row.id
                this.$refs.dbtable.reload(this.listDbApiParams)
            }
        },
        //表格选择后回调事件
        currentChange(selection) {
            this.selection = selection;
        },
        //添加
        database_add() {
            if (!this.selection) {
                this.$alert('请选择项目', "提示", {type: 'error'})
                return;
            }
            this.dialog.dbSave = true
            this.$nextTick(() => {
                this.$refs.dbDialog.open().setData(this.listDbApiParams)
            })
        },
        //编辑
        database_edit(row) {
            this.dialog.dbSave = true
            this.$nextTick(() => {
                this.$refs.dbDialog.open('edit').setData(row)
            })
        },
        //查看
        database_show(row) {
            this.dialog.dbSave = true
            this.$nextTick(() => {
                this.$refs.dbDialog.open('show').setData(row)
            })
        },
        //删除
        async database_del(row) {
            const res = await this.$API.design.database.delete(row.id);
            if (res.code === 200) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                await this.$alert(res.msg, "提示", {type: 'error'})
            }
        },
        //本地更新数据
        handleDbSaveSuccess(data, mode) {
            if (mode === 'add') {
                this.refreshDbList()
            } else if (mode === 'edit') {
                this.refreshDbList()
            }
        },
        refreshDbList() {
            this.$refs.dbtable.refresh()
        },
        drawerClose() {
            this.refreshDbList()
        },
        openDrawer(row) {
            this.drawer.projectId = row.projectId
            this.drawer.dbId = row.id
            this.drawer.dbType = row.dbType
            this.drawer.module = true
        },
        openDictDrawer(row) {
            this.drawer.projectId = row.id
            this.drawer.dict = true
        },
        //获取字段值
        async getDictMap() {
            const res = await this.$API.system.dict.detail.map(Object.values(this.props))
            this.dictMap = res.data
        },
        //列表字典转换
        formatter(row, column) {
            //获取字段编码
            const code = this.props[column.property];
            if (!code) {
                return;
            }
            //获取字段明细
            const dictList = this.dictMap[code];
            if (!dictList) {
                return;
            }
            //找到对应字典项
            const dict = dictList.find(dict => dict.value === row[column.property]);
            if (dict) {
                return <el-tag type={dict.type}>{dict.label}</el-tag>;
            }
        }
    }
}
</script>

<style>
</style>
